/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thing;

import Core.Vector2;
import FlyingType.Arrow;
import FlyingType.FireBullet;
import java.util.Random;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author THINH
 */
public class Monster extends Thing {
    //field

    int[] standSeq, goRightSeq, jumpSeq, shootSeq, shootNormalSeq, sitSeq;
    public int speed = 4;
    public Flying fly = null;
    protected Character human;
    private long shootStart = 0;
    private boolean shooting = false;
    public int range = 0;
    public int MAX_RANGE = 50;
    public int MAX_RANGE_SHOOT = MAX_RANGE/2;
    public int MIN_RANGE = 100;
    public int MAX_HEAL = 1000;
    public int health= 100;
    protected int damage = 10;
    protected int armor = 10;
    private int count = 0;
    //constructor

    public Monster() {
        super();
    }

    public Monster(String pathImage, int frameWidth, int frameHeight, int[] rightSeq, int[] shootSeq, Character h) {
        super(pathImage, frameWidth, frameHeight, rightSeq);
        this.goRightSeq = rightSeq;
        this.shootNormalSeq = shootSeq;
        this.human = h;
        sprite.defineReferencePixel(0, sprite.getHeight()/3);
    }
    //main func

    public void update() {
        //code here
        //update bullet
        if (fly != null) {
            fly.update();
            if (System.currentTimeMillis() - shootStart > 600) {
                fly.isLive = false;
            }
        }
        //shoot
        
        range++;
        if (range > MAX_RANGE) {
            range = -1 * MAX_RANGE;
        }
        //DistanceShoot();
           
        
        super.update();
    }
    //func

    protected void moveLeft() {
        if (status.value != status.GoLeft && !status.isJump()) {
            sprite.setFrameSequence(goRightSeq);
            int x = sprite.getX();
            int y = sprite.getY();
            sprite.setTransform(Sprite.TRANS_MIRROR);
            sprite.setPosition(x, y);
            status.value = status.GoLeft;
        }
        if (!status.isJump()) {
            vel.set(new Vector2(-1 * speed, vel.y));
        }
    }

    protected void moveRight() {
        if (status.value != status.GoRight && !status.isJump()) {
            sprite.setFrameSequence(goRightSeq);
            int x = sprite.getX();
            int y = sprite.getY();
            sprite.setTransform(Sprite.TRANS_NONE);
            sprite.setPosition(x, y);
            status.value = status.GoRight;
        }
        if (!status.isJump()) {
            vel.set(new Vector2(1 * speed, vel.y));
        }
    }

    private void Shoot() {
        sprite.setFrameSequence(shootNormalSeq);
        if (status.isLeft()) {
            status.value = status.ShootLeft;
        } else if (status.isRight()) {
            status.value = status.ShootRight;
        }
        shooting = true;
        shootStart = System.currentTimeMillis();
    }

    protected void Shoot(Vector2 direction) {
        if (fly != null) {
            fly.shoot(direction);
            fly.setPos(this.getSprite().getX(), this.getSprite().getY());
            this.Shoot();
            //flying.status.value = Status.ShootNormal;
        }
    }

    private void DistanceShoot() {
        int distance = 0;
        if(count == 0){
            distance = (this.getSprite().getX() - human.getSprite().getX());
        }
        if (Math.abs(distance) < MAX_RANGE_SHOOT && Math.abs(distance) > MIN_RANGE) {
            if (distance < 0) {
                if (status.value == status.GoLeft) {
                    range = -1 * range;
                } else {
                    //this.Shoot(new Vector2(13,0));
                }
            } else {
                if (status.value == status.GoLeft) {
                   //this.Shoot(new Vector2(-13,0));
                } else {
                    range = -1 * range;
                }
            }
        }
        else if(Math.abs(distance) <= MIN_RANGE)
        {
            if (count == 0) {
                //doi huong
                range = -1 * range;
                count = 10;
            } else {
                count--;
            }
                
        }
        
    }
    
    protected void CreateBullet()
    {
        
    }
    
    public int getdamage()
    {
        return damage;
    }
    
    public int getArmor()
    {
        return armor;
    }
}
